<template>
	<view style="min-height: 100vh;background: #F3F3F3;">
		<view class="navHead" :style="{'padding-top':statusH+'px'}">
			<view class="navHeadCon">
				<image src="../../static/index/back.png" class="back" @click="back"></image>
				<view class="navTit">选择团单</view>
				<view class="allChoice" @click="selectAll">全选</view>
			</view>
		</view>
		<view class="emptyBox" :style="{'padding-top':statusH+'px'}"></view>
		<view class="conWrap">
			<view class="conItem" v-for="(item,index) in list" :key="index" :data-index="index" @click="selectGroup">
				<view :class="['conItemL',item.choice?'active':'']">
					<image src="../../static/mine/right_white.png" v-if="item.choice"></image>
				</view>
				<view class="conItemR" v-if="type=='td'">{{item.name}}</view>
				<view class="conItemR" v-if="type=='cp'">{{item.title}}</view>
			</view>
		</view>
		<view class="submit" @click="submit">确认</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				statusH:0,
				page:1,
				moreFlag:false,
				list:[],
				allFlag:false,
				choiceList:[],
				type:""
			}
		},
		onReachBottom() {
			if(!this.moreFlag){
				return false;
			}
			this.moreFlag = false;
			this.page = this.page + 1;
			this.getList();
		},
		onLoad(options) {
      // #ifdef APP-PLUS
      var args = plus.runtime.arguments;
      if(args){
        app.$vm.setShareDetail(args);
      }
      // #endif
			console.log(options)
			uni.getSystemInfo({
				success:res=>{
					this.statusH = res.statusBarHeight;
				}
			})
			console.log(options)
			this.type = options.type;
			this.allFlag = options.flag == 'true' ? true : false;
			this.choiceList = uni.getStorageSync("choiceGroupData");
			this.getList();
		},
		onShow() {
			
		},
		methods: {
			submit(){
				let arr = [];
				for(let i=0;i<this.list.length;i++){
					if(this.list[i].choice){
						arr.push(this.list[i]);
					}
				}
				let pages = getCurrentPages();
				pages = pages[pages.length-2];
				pages.$vm.groupData = arr;
				pages.$vm.allFlag = this.allFlag;
				uni.navigateBack({
					delta:1
				})
			},
			back(){
				uni.navigateBack({
					delta:1
				})
			},
			selectAll(){
				if(this.allFlag){
					for(let i=0;i<this.list.length;i++){
						let obj = this.list[i];
						obj.choice = false;
						this.list.splice(i,1,obj);
					}
					this.allFlag = false;
				}else{
					for(let i=0;i<this.list.length;i++){
						let obj = this.list[i];
						obj.choice = true;
						this.list.splice(i,1,obj);
					}
					this.allFlag = true;
				}
			},
			getAll(){
				let flag = true;
				for(let i=0;i<this.list.length;i++){
					if(!this.list[i].choice){
						flag = false;
					}
				}
				this.allFlag = flag;
			},
			selectGroup(e){
				let index = e.target.dataset.index || e.currentTarget.dataset.index;
				let obj = this.list[index];
				obj.choice = !obj.choice;
				this.list.splice(index,1,obj);
				this.getAll();
				// this.$forceUpdate();
			},
			getList() {
				let obj = {};
				obj.page = this.page;
				obj.data = {};
				obj.data.user_id = uni.getStorageSync("userId");
				if(this.type=='td'){
					this.$u.api.shopPackageList(obj).then(res => {
						console.log(res)
						if (this.page == 1) {
							let arrs = res.data.list.data.map(cur => {
								cur.allNum = Number(cur.sales_volume) + Number(cur.num);
								cur.choice = this.allFlag;
								return cur;
							});
							if(!this.allFlag){
								for(let i=0;i<arrs.length;i++){
									for(let j=0;j<this.choiceList.length;j++){
										if(arrs[i].id==this.choiceList[j].id){
											arrs[i].choice = true;
										}
									}
								}
							}
							this.list = arrs;
						} else {
							let arrs = this.list.concat(res.data.list.data.map(cur => {
								cur.allNum = Number(cur.sales_volume) + Number(cur.num);
								cur.choice = this.allFlag;
								return cur;
							}));
							if(!this.allFlag){
								for(let i=0;i<arrs.length;i++){
									for(let j=0;j<this.choiceList.length;j++){
										if(arrs[i].id==this.choiceList[j].id){
											arrs[i].choice = true;
										}
									}
								}
							}
							this.list = arrs;
						}
						if (this.page < res.data.list.last_page) {
							this.moreFlag = true;
						} else {
							this.moreFlag = false;
						}
					})
				}else{
					this.$u.api.shopEvaluationList(obj).then(res => {
						console.log(res)
						if (this.page == 1) {
							let arrs = res.data.list.data.map(cur => {
								cur.choice = this.allFlag;
								return cur;
							});
							if(!this.allFlag){
								for(let i=0;i<arrs.length;i++){
									for(let j=0;j<this.choiceList.length;j++){
										if(arrs[i].id==this.choiceList[j].id){
											arrs[i].choice = true;
										}
									}
								}
							}
							this.list = arrs;
						} else {
							let arrs = this.list.concat(res.data.list.data.map(cur => {
								cur.choice = this.allFlag;
								return cur;
							}));
							if(!this.allFlag){
								for(let i=0;i<arrs.length;i++){
									for(let j=0;j<this.choiceList.length;j++){
										if(arrs[i].id==this.choiceList[j].id){
											arrs[i].choice = true;
										}
									}
								}
							}
							this.list = arrs;
						}
						if (this.page < res.data.list.last_page) {
							this.moreFlag = true;
						} else {
							this.moreFlag = false;
						}
					})
				}
				
			}
		}
	}
</script>

<style scoped>
	.submit{
		width: 100%;
		height: 100upx;
		line-height: 100upx;
		text-align: center;
		background: #FFAC3F;
		color: white;
		font-size: 28upx;
		position: fixed;
		bottom: 0;
		left: 0;
	}
	
	
	.conWrap{
		padding: 16upx 24upx 116upx;
	}
	.conItem{
		width: 100%;
		height: 88upx;
		display: flex;
		align-items: center;
		border-radius: 8upx;
		background: white;
		box-sizing: border-box;
		padding: 0 24upx;
		margin-bottom: 16upx;
	}
	.conItemL{
		width: 24upx;
		height: 24upx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
		border-radius: 4upx;
		border: 2upx solid #BFBFBF;
		margin-right: 22upx;
	}
	.conItemL.active{
		border-color: #FFAC3F;
		background: #FFAC3F;
	}
	.conItemL>image{
		width: 12upx;
		height: 12upx;
	}
	.conItemR{
		width: calc(100% - 46upx);
		font-size: 28upx;
		line-height: 88upx;
		color: #333333;
	}
	.conItem:last-child{
		margin-bottom: 0;
	}
	
	
	
	.emptyBox{
		width: 100%;
		height: 88upx;
		box-sizing: content-box;
	}
	.navHead{
		position: fixed;
		width: 100%;
		left: 0;
		top: 0;
		background: white;
		z-index: 999;
	}
	.navHeadCon{
		width: 100%;
		height: 88upx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
	}
	.back{
		width: 48upx;
		height: 48upx;
		position: absolute;
		left: 24upx;
		top: 50%;
		transform: translateY(-50%);
	}
	.navTit{
		font-size: 28upx;
		color: #333333;
		line-height: 88upx;
		text-align: center;
	}
	.allChoice{
		position: absolute;
		right: 32upx;
		font-size: 28upx;
		line-height: 88upx;
		top: 0;
		color: #333333;
	}
</style>
